package com.tms.app.tms.ui.base.presenter;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.GpsBean;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2017/11/27 0027.
 */

public class BaseListPresenter extends AbsBaseListP {
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        if (requestType==2017){
            try {
                DbManager db = x.getDb(MyApp.getDaoConfig());
                if (db == null)
                    return;
               // db.delete(GpsBean.class, WhereBuilder.b("status","=",0));
                db.update(GpsBean.class, WhereBuilder.b("status","=",0),new KeyValue("status",1));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public   void uploadGps(){
        requestType=2017;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            if (db == null)
                return;
            List<GpsBean> gpsBeanList=db.selector(GpsBean.class).where("status","=",0).findAll();
            if (gpsBeanList!=null&&gpsBeanList.size()>0)
            {
                String json=gson.toJson(gpsBeanList);
                RequestParams params=new RequestParams(Constant.GPS);
                // params.addBodyParameter("token",token);
                params.setAsJsonContent(true);
                params.addBodyParameter("gps",json);
                getObjectFromServer(params,1);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }



    public boolean uploadGps(String token) {
        requestType = 2017;
        try {
            DbManager db = x.getDb(MyApp.getDaoConfig());
            if (db == null)
                return false;
            List<GpsBean> gpsBeanList = db.selector(GpsBean.class).where("status", "=", 0).findAll();
            if (gpsBeanList != null && gpsBeanList.size() > 0) {
                for (GpsBean gpsBean : gpsBeanList) {
                    uploadGps(
                            token,
                            gpsBean.getDriverId()+"",
                            gpsBean.getCarId()+"",
                            gpsBean.getTrailerId()+"",
                            gpsBean.getPlanId()+"",
                            gpsBean.getLon(),
                            gpsBean.getLat(),
                            gpsBean.getTime()+"");
                }
                return true;
            }else {

            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return false;
    }



    public   void uploadGps( String token,
                            String driverId, String carId, String trailerId, String planId,
                            String longitude, String latitude,String time){
        requestType=2017;
        try {

                RequestParams params=new RequestParams(Constant.GPS2);
                 params.addBodyParameter("token",token);
                params.addBodyParameter("driverId",driverId);
                params.addBodyParameter("carId",carId);
                params.addBodyParameter("trailerId",trailerId);
                params.addBodyParameter("planId",planId);
                params.addBodyParameter("longitude",longitude);
                params.addBodyParameter("latitude",latitude);
                params.addBodyParameter("time",time);
            //    params.addBodyParameter("showLoading",2017+"");
                x.http().post(params,this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void getBDToken(){
        requestType=2015;
        RequestParams params=new RequestParams("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=frCV293yOXOOVIVp7YSzzIXF&client_secret=zOx2H86vXAOK0HYzfh722G9ziSGOGQci");
        x.http().get(params, new CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    String BDToken=jsonObject.getString("access_token");
                    if (!StringUtil.isEmpty(BDToken)&&BDToken.length()>5)
                        SharePreferenceUtil.setValue(MyApp.getInstance().getApplicationContext(),"BDToken",BDToken);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }

}
